package edu.mta.pokerstats;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.cloudfoundry.services.CloudFoundryServices;
import org.cloudfoundry.services.ServiceManager;

public class Player {
	private int m_Id;
	private String m_Username;
	private String m_Password;
	private String m_Name;
	private Date m_Birthday;
	private String m_JoinStatus;
	private int m_TeamId;

	public Player(int i_Id, String i_Username, String i_Password, String i_Name,
			Date i_Birthday, String i_JoinStatus, int i_TeamId) {
		m_Id = i_Id;
		m_Username = i_Username;
		m_Password = i_Password;
		m_Name = i_Name;
		m_Birthday = i_Birthday;
		m_JoinStatus = i_JoinStatus;
		m_TeamId = i_TeamId;
	}

	public Player(ResultSet i_RS) {
		try {
			m_Id = i_RS.getInt("id");
			m_Username = i_RS.getString("username");
			m_Password = i_RS.getString("password");
			m_Name = i_RS.getString("name");
			m_Birthday = i_RS.getDate("birthday");
			m_JoinStatus = i_RS.getString("joinStatus");
			m_TeamId = i_RS.getInt("teamID");
		} catch (SQLException e) {
			new Player(0, "", "", "", new Date(0), "not found", 0);
		}
	}

	public int getId() {
		return m_Id;
	}

	public void setId(int m_Id) {
		this.m_Id = m_Id;
	}

	public String getUsername() {
		return m_Username;
	}

	public void setUsername(String i_Username) {
		this.m_Username = i_Username;
	}

	public String getPassword() {
		return m_Password;
	}

	public void setPassword(String m_Password) {
		this.m_Password = m_Password;
	}

	public String getName() {
		return m_Name;
	}

	public void setName(String m_Name) {
		this.m_Name = m_Name;
	}

	public Date getBirthday() {
		return m_Birthday;
	}

	public void setBirthday(Date m_Birthday) {
		this.m_Birthday = m_Birthday;
	}

	public String getJoinStatus() {
		return m_JoinStatus;
	}

	public void setJoinStatus(String m_JoinStatus) {
		this.m_JoinStatus = m_JoinStatus;
	}

	public int getTeamId() {
		return m_TeamId;
	}

	public void setTeamId(int m_TeamId) {
		this.m_TeamId = m_TeamId;
	}

	public boolean InsertToDatabase() {
		int newId;
		boolean success = false;
		newId = ServiceManager.INSTANCE.InsertQuery(getInsertString());
		if (newId >= 0) {
			m_Id = newId;
			success = true;
		}
		return success;
	}

	public boolean UpdateInDatabase() {
		return ServiceManager.INSTANCE.UpdateQuery(getUpdateString());
	}

	public boolean DeleteFromDatabase() {
		return ServiceManager.INSTANCE.DeleteFromDatabase("players", m_Id);
	}

	private String getInsertString() {
		String result = "insert into players values(" + null + "," + "'"
				+ m_Username + "'," + "'" + m_Password + "'," + "'" + m_Name + "',";
		if (m_Birthday == null){
			result += m_Birthday + ",";
		} else {
			result += "'" + m_Birthday + "',";
		}
		result += "'" + m_JoinStatus + "'," + m_TeamId + ")";

		return result;
	}

	private String getUpdateString() {
		String result = "update players SET " + "username = '" + m_Username + "',"
				+ "password = '" + m_Password + "'," + "name = '" + m_Name + "',";
		if (m_Birthday == null){
			result += "birthday = " + m_Birthday + ",";
		} else {
			result += "birthday = '" + m_Birthday + "',";
		}
		result += "joinStatus = '" + m_JoinStatus + "',"
				+ "teamID = " + m_TeamId + " WHERE id=" + m_Id;

		return result;
	}
	
	public boolean isTeamLeader() {
		return Team.getTeamById(m_TeamId).getTeamLeaderId() == m_Id;
	}

	public ArrayList<GameRecord> GetGameRecordsForPlayer() {
		ArrayList<GameRecord> gameRecords = new ArrayList<GameRecord>();
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from gameRecords where playerID=" + m_Id
					+ " order by gameID desc;";
			ResultSet rs = s.executeQuery(query);
			while (rs.next()) {
				GameRecord tempGR = new GameRecord(rs);
				if (tempGR != null) {
					gameRecords.add(tempGR);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return gameRecords;
	}

	public static Player getPlayerById(int i_Id) {
		Player returnValue = null;
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from players where id =" + i_Id;
			ResultSet rs = s.executeQuery(query);
			rs.first();
			returnValue = new Player(rs);
		} catch (Exception e) {
			returnValue = null;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return returnValue;
	}

	public static Player getPlayerByUsername(String i_Username) {
		Player returnValue = null;
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from players where username like '" + i_Username
					+ "'";
			ResultSet rs = s.executeQuery(query);
			rs.first();
			returnValue = new Player(rs);
		} catch (Exception e) {
			returnValue = null;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return returnValue;
	}
}